package zym.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.DeptDAO;
import dao.DutyDAO;
import dao.EmpDAO;
import dao.impl.DeptDAOImpl;
import dao.impl.DutyDAOImpl;
import dao.impl.EmpDAOImpl;

import zym.service.TansferMag;

import model.Dept;
import model.Duty;
import model.Emp;
import model.EmpTransfer;

public class addServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		TansferMag tf = new TansferMag();
		String empno=request.getParameter("empno");
		String depy1=request.getParameter("b_dept");
		String depy2=request.getParameter("dept_n2");
		String duty1=request.getParameter("b_duty");
		String duty2=request.getParameter("duty_n2");
		Date time=new Date();
		try {
			time = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("d_time"));
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		
		String reason = request.getParameter("reason");
		
		System.out.println(depy1);

		EmpDAO empdao=new EmpDAOImpl();
		DeptDAO deptdao=new DeptDAOImpl();
		DutyDAO dutydao=new DutyDAOImpl();
		
		
		EmpTransfer e = new EmpTransfer();
		int id=empdao.getIDByEmpno(empno);
		
		
		Emp emp=empdao.getEmpById(id);
		e.setEmp(emp);
		
		System.out.println(emp.getId());
		
		Dept beforedept=deptdao.getDeptByName(depy1);
		e.setBeforedept(beforedept);
		
		Dept afterdept=deptdao.getDeptById(Integer.parseInt(depy2));
		e.setAfterdept(afterdept);
		
		Duty beforeduty = dutydao.getDutyByName(duty1);
		e.setBeforeduty(beforeduty);
		
		Duty afterduty = dutydao.getDutyById(Integer.parseInt(duty2));
		e.setAfterduty(afterduty);
		
		e.setTime(time);
		e.setReason(reason);
		
		
		tf.addTransfer(e);
		request.getRequestDispatcher("/TransferServlet").forward(request, response);
	}

}
